﻿function VisitViewModel() {
    var visitVM =
            {
                Visit: ko.observable()
                , VisitID: ko.observable()
                , Title: ko.observable()
                , UrlName: ko.observable()
                , SumUp: ko.observable()
                , Text: ko.observable()
                , Visible: ko.observable()
                , Latitude: ko.observable()
                , Longitude: ko.observable()
                , Pictures: ko.observableArray([])
                , IsLoaded: ko.observable(false)
                , IsLoading: ko.observable(false)

                , Load: function (forcedReload) {

                    if (forcedReload || (!this.IsLoading() && !this.IsLoaded())) {
                        var service = new mal.balades.Web.Administration.VisitService();
                        service.GetVisit(this.VisitID(), this.onGetVisitSuccess, null, this);
                    }
                    else {
                        this.SetMapLocation();
                    }
                }

                , onGetVisitSuccess: function (visit, userstate) {
                    userstate.IsLoading(false);
                    userstate.IsLoaded(true);
                    userstate.Visit(visit);
                    userstate.VisitID(visit.VisitID);
                    userstate.Title(visit.Title);
                    userstate.UrlName(visit.UrlName);
                    userstate.SumUp(visit.SumUp);
                    userstate.Text(visit.Text);
                    userstate.Visible(visit.Visible);
                    userstate.Latitude(visit.Latitude);
                    userstate.Longitude(visit.Longitude);

                    visit.Pictures = visit.Pictures.sort(function (v1, v2) {
                        if (v1.Rank > v2.Rank)
                            return 1;
                        else
                            return -1;
                    });

                    for (var i = 0; i < visit.Pictures.length; i++) {
                        var picture = new PictureViewModel();
                        picture.Load(userstate, visit.Pictures[i]);
                        userstate.Pictures.push(picture);
                    }

                    //                    for (var i = 0; i < 10 - visit.Pictures.length; i++) {
                    //                        var picture = new pictureViewModel();
                    //                        picture.Load(userstate, null);
                    //                        userstate.Pictures.push(picture);
                    //                    }

                    userstate.SetMapLocation();
                }

                , AddPicture: function () {
                    var picture = new PictureViewModel();
                    picture.Load(this, null);
                    this.Pictures.push(picture);
                }

                /// <summary>
                /// Convert VisitViewModel object into Visit.
                /// </summary>
                , Unload: function () {
                    var serverVisit = new mal.balades.Business.DataModel.Visit();

                    serverVisit.VisitID = this.VisitID();
                    serverVisit.Title = this.Title();
                    serverVisit.UrlName = this.UrlName();
                    serverVisit.SumUp = this.SumUp();
                    serverVisit.Text = this.Text();
                    serverVisit.Visible = this.Visible();
                    serverVisit.Latitude = this.Latitude();
                    serverVisit.Longitude = this.Longitude();
                    serverVisit.Pictures = [];

                    for (var i = 0; i < this.Pictures().length; i++) {
                        var pictureVM = this.Pictures()[i];
                        if (!pictureVM.IsDeleted()) {
                            serverVisit.Pictures.push(pictureVM.Unload());
                        }
                    }

                    return serverVisit;
                }

                /// <summary>
                /// Send the visit location to the map.
                /// </summary>
                , SetMapLocation: function () {
                    map.Content.MapHelper.SetLocation(this.Latitude(), this.Longitude());
                }

                /// <summary>
                /// Method called when the pictures list is sorted.
                /// </summary>   
                , PicturesListSorted: function (pictureIds) {
                    for (var i = 0; i < pictureIds.length; i++) {
                        for (var j = 0; j < this.Pictures().length; j++) {
                            if (pictureIds[i].replace("picture-", "") == this.Pictures()[j].PictureID()) {
                                this.Pictures()[j].Rank(i + 1);
                            }
                        }
                    }
                }


               , Cancel: function () {
                   this
               }
            };            

            visitVM.IsDirty = ko.dependentObservable(function () {
                if (this.Visit() != null) {
                    if (this.Visit().Title != this.Title()
                || this.Visit().UrlName != this.UrlName()
                || this.Visit().SumUp != this.SumUp()
                || this.Visit().Text != this.Text()
                || this.Visit().Visible != this.Visible()
                || this.Visit().Latitude != this.Latitude()
                || this.Visit().Longitude != this.Longitude())
                        return true;
                }

                for (var i = 0; i < this.Pictures().length; i++) {
                    if (this.Pictures()[i].IsDirty())
                        return true;
                }

                return false;
            }, visitVM);

            visitVM.PicturesToShow = ko.dependentObservable(function () {

                return ko.utils.arrayFilter(this.Pictures(), function (picture) {
                    return !picture.IsDeleted();
                });
            }, visitVM);
    return visitVM;
}
